/**
 * @param {number[]} nums
 * @return {number[][]}
 * 这个方法是先找到所有的两个数的和，比较费时间和空间
 */
var threeSum = function(nums) {
    nums.sort();
    let numsLength = nums.length;
    let two = new Map();
    for(let i=0;i<numsLength;i++){
        for(let j=i+1;j<numsLength;j++){
            var key = 0-(nums[i]+nums[j]);
            if(two.has(key)){
                two.get(key).push([[nums[i],nums[j]],i,j]);
            }else
                two.set(key, [[[nums[i],nums[j]],i,j]]);
        }
    }
    //console.log(two);
    let result = new Map();
    nums.forEach((v,k)=>{
        if(two.has(v)){
            var re;
            two.get(v).forEach(v1=>{
                re = v1;
                if(re[1]==k||re[2]==k)return;
                re = re[0].slice();
                re.push(v);
                re.sort();
                result.set(re.join(),re);
            });
        }
    });
    return Array.from(result.values());
};

console.log(threeSum([-1, 0, 1, 2, -1, -4]));
